#!/usr/bin/bash

build() {
    local mod
    local symlink

    # device mapper modules
    for mod in dm-mod dm-snapshot dm-mirror dm-cache dm-cache-smq dm-thin-pool; do
        add_module "$mod"
    done

    if command -v add_systemd_unit >/dev/null; then
        # device mapper and lvm2 systemd units
        # binaries and libraries are pull automatically, except ...
        add_systemd_unit "lvm2-pvscan@.service"
    fi

    # binaries from lvm2
    add_binary "lvm"

    # beinaries from device-mapper
    add_binary "dmsetup"

    # from thin-provisioning-tools
    add_binary "pdata_tools"
    for symlink in cache_{check,dump,metadata_size,repair,restore} thin_{check,delta,dump,ls,metadata_size,repair,restore,rmap,trim}; do
        add_symlink "/usr/bin/${symlink}" "pdata_tools"
    done

    # udev rules and lvm configuration
    add_file "/usr/lib/udev/rules.d/10-dm.rules"
    add_file "/usr/lib/udev/rules.d/11-dm-lvm.rules"
    add_file "/usr/lib/udev/rules.d/13-dm-disk.rules"
    add_file "/usr/lib/udev/rules.d/95-dm-notify.rules"
    add_file "/usr/lib/initcpio/udev/11-dm-initramfs.rules" "/usr/lib/udev/rules.d/11-dm-initramfs.rules"
    add_file "/etc/lvm/lvm.conf"
    sed -i '/^\smonitoring =/s/1/0/' "${BUILDROOT}/etc/lvm/lvm.conf"

    # this udev rule is specific for systemd and non-systemd systems
    if command -v add_systemd_unit >/dev/null; then
        add_file "/usr/lib/udev/rules.d/69-dm-lvm-metad.rules"
    else
        add_file "/usr/lib/initcpio/udev/69-dm-lvm-metad.rules" "/usr/lib/udev/rules.d/69-dm-lvm-metad.rules"
    fi
}

help() {
  cat <<HELPEOF
This hook enables LVM2 volumes in initramfs.
HELPEOF
}
